<template>
     <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="电影名称" prop="name" >
        <a-input v-model="form.name" placeholder="请输入电影名称" />
      </a-form-model-item>
      <a-form-model-item label="电影分类" prop="classify" >
          <a-select
            mode="single"
            v-model="form.classify"
            placeholder="请选择"
          >
            <a-select-option v-for="(d, index) in memberOptions" :key="index" :value="d.value">
              {{ d.name }}
            </a-select-option>
          </a-select>
      </a-form-model-item>
      <a-form-model-item label="简介概要" prop="synopsis">
        <a-input v-model="form.synopsis" type="textarea" />
      </a-form-model-item>
      <!-- <a-form-model-item label="上映时间" prop="updateTime" >
        <a-date-picker style="width: 100%" v-model="form.updateTime" format="YYYY-MM-DD HH:mm:ss" allow-clear/>
      </a-form-model-item> -->
      <a-form-model-item label="封面图片" prop="coverUrl" >
        <file-upload v-model="form.coverUrl" type="image"></file-upload>
      </a-form-model-item>
    
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
import { saveMovie,getMovie,updateMovie } from "@/services/movie";
export default {
  name: 'CreateForm',
  props: {
    
  },
  components: {
  },
  data () {
    return {
      submitLoading: false,
      formTitle: '',
      memberOptions:[
        {"name":"悬疑","value":"悬疑"},
        {"name":"喜剧","value":"喜剧"},
        {"name":"剧情","value":"剧情"},
        {"name":"犯罪","value":"犯罪"},
        {"name":"惊悚","value":"惊悚"},
        {"name":"动作","value":"动作"},
        {"name":"科幻","value":"科幻"}
    ] ,
      // 表单参数
      form: {
        name: null,
        classify: null,
        updateTime: null,
        coverUrl: null,
        synopsis: null
      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
      }
    }
  },
  filters: {
  },
  created () {
    
  },
  computed: {
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.formType = 1
      this.form = {
        name: null,
        classify: null,
        updateTime: null,
        coverUrl: null,
      }
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
      console.log(row)
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      this.reset()
      this.formType = 2
      const id = row ? row.id : ids
      getMovie(id).then(res=>{
        this.form = res.data.data;
        console.log("返回数据",res);
        this.open = true
        this.formTitle = '修改'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined && this.form.id !== null) {
            console.log("进入修改");
            updateMovie(this.form).then(res=>{
              console.log(res);
              this.$message.success('修改成功',3)
              this.open = false
              this.$emit('ok')
              this.submitLoading = false;
            })
          }else{
            saveMovie(this.form).then(res => {
              console.log(res);
              this.$message.success('新增成功',3)
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }

        } else {
          return false
        }
      })
    }
  }
}
</script>
